package com.qb.hotelTV.base;

import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.ViewModelProvider;

import java.lang.ref.WeakReference;

/**
 * 基础Activity类 - 优化版
 * 支持MVVM架构，防止内存泄漏，提供通用功能
 */
public abstract class BaseActivity<T extends ViewDataBinding, V extends BaseViewModel> extends FragmentActivity {
    
    protected T binding;
    protected V viewModel;
    private WeakReference<BaseActivity> activityRef;
    
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // 创建弱引用防止内存泄漏
        activityRef = new WeakReference<>(this);
        
        // 初始化DataBinding
        binding = DataBindingUtil.setContentView(this, getLayoutId());
        binding.setLifecycleOwner(this);
        
        // 初始化ViewModel
        viewModel = new ViewModelProvider(this).get(getViewModelClass());
        
        // 设置ViewModel到DataBinding
        setViewModelToBinding();
        
        // 观察通用的LiveData
        observeCommonLiveData();
        
        // 初始化视图
        initView();
        
        // 初始化数据
        initData();
    }
    
    /**
     * 获取布局ID
     */
    protected abstract int getLayoutId();
    
    /**
     * 获取ViewModel的Class
     */
    protected abstract Class<V> getViewModelClass();
    
    /**
     * 设置ViewModel到DataBinding
     */
    protected abstract void setViewModelToBinding();
    
    /**
     * 初始化视图
     */
    protected abstract void initView();
    
    /**
     * 初始化数据
     */
    protected abstract void initData();
    
    /**
     * 观察通用的LiveData
     */
    private void observeCommonLiveData() {
        // 观察错误消息
        viewModel.errorMessage.observe(this, message -> {
            if (message != null && !message.isEmpty()) {
                showErrorMessage(message);
            }
        });
        
        // 观察Toast消息
        viewModel.toastMessage.observe(this, message -> {
            if (message != null && !message.isEmpty()) {
                showToast(message);
            }
        });
        
        // 观察加载状态
        viewModel.isLoading.observe(this, this::onLoadingStateChanged);
    }
    
    /**
     * 显示错误信息
     */
    protected void showErrorMessage(String message) {
        Toast.makeText(this, "错误: " + message, Toast.LENGTH_LONG).show();
    }
    
    /**
     * 显示Toast信息
     */
    protected void showToast(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
    
    /**
     * 加载状态改变
     */
    protected void onLoadingStateChanged(Boolean isLoading) {
        // 子类可以重写此方法来处理加载状态
    }
    
    /**
     * 安全执行UI操作
     */
    protected void runOnUiThreadSafely(Runnable action) {
        if (activityRef.get() != null && !isFinishing() && !isDestroyed()) {
            runOnUiThread(action);
        }
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        
        // 清理引用
        if (activityRef != null) {
            activityRef.clear();
        }
        
        // 清理DataBinding
        if (binding != null) {
            binding.unbind();
        }
    }
} 